Ext.regController("MainController", {
	"index": function(opt) {
		if (!CeleBrick.views.mainView)
			CeleBrick.views.mainView = new CeleBrick.views.MainView();
		
		Ext.dispatch({
			controller: this,
			action: "gameStart"
		});
	},

	"gameStart": function(opt) {
		divStage = CeleBrick.views.stageView.body.child("#stage");
		divCountDown = divStage.child("#count-down");

		divCeleb = new Array();
		divCritical = new Array();

		$CELEB.max_clone = divStage.child(".celebs").dom.children.length;

		for (i = 0; i < $CELEB.max_clone; i++) {
			divCeleb.push(divStage.child(".celebs > #celeb-" + i));
			divCritical.push(divStage.child(".criticals > #critical-" + i));
		}
		divBrick = divStage.child("#brick");
		divLevel = divStage.child("#level");
		divDifficulty = divStage.child("#difficulty");
		divScore = divStage.child("#score");
		divHighScore = divStage.child("#high-score");
		audPlayback = CeleBrick.views.mainView.audPlayBack;
		iTotalScore = 0;
		iCurLevel = $GAME.def_level;
		iCurMode = $GAME.def_mode;

		Ext.dispatch({
			controller: CeleBrick.controllers.stageController,
			action: "viewInit"
		});
		Ext.dispatch({
			controller: CeleBrick.controllers.stageController,
			action: "stageInit",
			mode: iCurMode,
			level: iCurLevel,
			totalScore: iTotalScore
		});
		Ext.dispatch({
			controller: CeleBrick.controllers.stageController,
			action: "stageAnimate"
		});

		CeleBrick.views.mainView.setActiveItem(CeleBrick.views.stageView);
	},

	"levelFinish": function(opt) {
		audPlayback.pause();
		var nextLevel = "";
		if (iCurLevel == $MODE.max_level[iCurMode])
			if (iCurMode == $GAME.max_mode)
				nextLevel = "Start new game";
			else
				nextLevel = "Higher difficulty";
		else
			nextLevel = "Next level";

		if (nextLevel == "Start new game")
			Ext.Msg.alert("Game completed!", "Congratulation! You've completed all levels", function(btn) {
			}, this);
		
		if (!this.actionSheet) {
			this.actionSheet = new Ext.ActionSheet({
				items: [{
					text: "Retry",
					scope: this,
					handler: function() {
						Ext.dispatch({
							controller: CeleBrick.controllers.stageController,
							action: "stageInit",
							mode: iCurMode,
							level: iCurLevel,
							totalScore: iTotalScore
						});
						this.actionSheet.hide();
					}
				}, {
					text: "Next level",
					ui: "confirm",
					scope: this,
					handler: function() {
						Ext.dispatch({
							controller: this,
							action: "newLevel"
						});			
						this.actionSheet.hide();
					}
				}, {
					text: "Back to main menu",
					ui: "decline",
					scope: this,
					handler: function() {
						this.actionSheet.hide();
					}
				}]
			});
		}

		this.actionSheet.items.items[1].setText(nextLevel);
		this.actionSheet.show();
	},

	"newLevel": function(opt) {
		var nextLevel = iCurLevel+1, nextMode = iCurMode;
		if (iCurLevel == $MODE.max_level[iCurMode]) {
			if (iCurMode == $GAME.max_mode) {
				nextMode = $GAME.def_mode;
				nextLevel = $GAME.def_level;
				iTotalScore = iLevelScore = 0;
			}
			else {
				nextMode = iCurMode+1;
				nextLevel = $GAME.def_level;
			}
		}

		Ext.dispatch({
			controller: CeleBrick.controllers.stageController,
			action: "stageInit",
			mode: nextMode,
			level: nextLevel,
			totalScore: iTotalScore + iLevelScore
		});
	}
});

CeleBrick.controllers.mainController = Ext.ControllerManager.get("MainController");